<template>
  <div class="adminPage">
    <!-- 搜索框 -->
    <div class="search_area">
      <el-form
        :model="params"
        label-width="80px"
        style="display: flex; margin-bottom: 50px"
      >
        <el-form-item label="用户名">
          <el-input
            clearable
            placeholder="请输入用户名"
            v-model="params.username"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input
            clearable
            placeholder="请输入昵称"
            v-model="params.realName"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item label="电话号">
          <el-input
            clearable
            placeholder="请输入电话号"
            v-model="params.phone"
            style="width: 200px"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item>
          <div class="search-buttons">
            <el-button type="warning" style="margin-left: 10px" @click="search"
              >查询</el-button
            >
            <el-button
              type="primary"
              style="margin-left: 10px"
              @click="addUser"
              v-if="$store.state.userInfo.role === 'ROLE_ADMIN'"
              >新增</el-button
            >
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--表单数据-->
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{
          background: '#fafafa',
          color: '#262626',
          fontSize: '14px',
          fontWeight: '500',
          height: '10px',
        }"
      >
        <el-table-column
          prop="username"
          label="用户名"
          align="center"
        ></el-table-column>
        <el-table-column prop="password" label="密码" align="center">
        </el-table-column>
        <el-table-column prop="realName" label="昵称" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="电话" align="center">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" align="center">
        </el-table-column>
        <el-table-column prop="role" label="角色" align="center">
          <template slot-scope="scope">
            {{ getRole(scope.row.role) }}
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column
          fixed="right"
          label="操作"
          align="center"
          v-if="$store.state.userInfo.role === 'ROLE_ADMIN'"
        >
          <template slot-scope="scope">
            <el-button type="primary" @click="updateUser(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              title="确定删除吗？"
              @confirm="deleteUser(scope.row)"
            >
              <el-button slot="reference" type="warning">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="block" style="margin-top: 20px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNo"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 新增用户/修改用户 抽屉 -->
    <el-drawer
      :title="drawerTitle"
      :visible.sync="drawerVisible"
      :direction="direction"
      :before-close="handleClose"
    >
      <admin-drawer
        :isEdit="isEdit"
        :record="currentData"
        @success="onSuccess"
        @close="drawerVisible = false"
      ></admin-drawer>
    </el-drawer>
  </div>
</template>

<script>
import { userlist, deleteUser } from "../../../api/sys/user";
import AdminDrawer from "../admin/components/AdminDrawer.vue";

export default {
  name: "adminInfo",
  components: { AdminDrawer },
  data() {
    return {
      //分页查询参数
      params: {
        pageSize: 10,
        pageNo: 1,
        username: "",
        phone: "",
        realName: "",
      },
      tableData: [], //表单数据
      loading: "", //加载状态
      total: "", //数据条数
      isEdit: false, //是否是编辑状态
      drawerVisible: false,
      drawerTitle: "", //抽屉标题
      currentData: {}, //当前行数据
    };
  },
  created() {
    this.loadData();
  },
  methods: {
    //加载用户数据
    async loadData() {
      this.loading = true;
      try {
        // const param = {
        //   ...this.params,
        //   username: this.params.username ? `*${this.params.username}*` : "",
        //   realName: this.params.realName ? `*${this.params.realName}*` : "",
        // };
        // console.log(param);
        const res = await userlist(this.params);
        if (res.code === 200) {
          // this.tableData = res.data.list || [];
          this.tableData = res.data.list.filter((item) => {
            return item.role !== "ROLE_ADMIN";
          });
          this.total = res.data.total || 0;
        } else if (res.code === 10001) {
          this.$message.error(res.message);
          //清理登录信息
          this.$store.dispatch("Logout");
        }
      } finally {
        this.loading = false;
      }
    },
    //根据输入模糊查询
    search() {
      this.params.pageNo = 1;
      this.loadData();
    },
    //页码尺寸变化
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.pageNo = 1;
      this.loadData();
    },
    //当前页变化
    handleCurrentChange(pageNo) {
      this.params.pageNo = pageNo;
      this.loadData();
    },
    //新增用户
    addUser() {
      this.currentData = {};
      this.drawerVisible = true;
      this.drawerTitle = "新增用户";
    },

    //删除用户
    deleteUser(data) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const id = data.id;
        deleteUser(id).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.loadData();
          }
        });
      });
    },

    //修改用户
    updateUser(data) {
      this.isEdit = true;
      this.drawerVisible = true;
      this.drawerTitle = "修改用户";
      this.currentData = data; //当前行数据
      console.log(this.currentData);
    },
    //成功
    onSuccess() {
      this.loadData();
    },
    //得到对应的角色
    getRole(role) {
      switch (role) {
        case "ROLE_USER":
          return "普通用户";
        case "ROLE_ADMIN":
          return "管理员";
        case "ROLE_STUDENT":
          return "学生";
        default:
          return "未知";
      }
    },
  },
};
</script>


<style>
.adminPage {
  min-height: 100%;
  background-color: transparent;
}

.el-button {
  margin: 5px;
}
</style>